<?php

namespace App\Http\Controllers\Admin\System;

use App\Enums\ResponseEnum;
use App\Http\Controllers\AdminController;
use App\Library\AttachmentTools;
use App\Models\System\Sites;
use Illuminate\Http\Request;

class ConfigController extends AdminController
{
    public function data(Request $request)
    {
        $siteKey = $request->get('siteKey');
        $config = Sites::GetPluginSet(trim($siteKey));
        if ($siteKey === 'uploadConfig') {
            $config['image_size'] = !empty($config['image_size']) ? (int)$config['image_size'] : AttachmentTools::IMAGE_SIZE;
            $config['file_size'] = !empty($config['file_size']) ? (int)$config['file_size'] : AttachmentTools::FILE_SIZE;
            $config['image_type'] = !empty($config['image_type']) ? (array)$config['image_type'] : AttachmentTools::IMAGE_TYPE;
            $config['file_type'] = !empty($config['file_type']) ? (array)$config['file_type'] : AttachmentTools::FILE_TYPE;
            $config['upload_max_filesize'] = ini_get('upload_max_filesize');
            $config['post_max_size'] = ini_get('post_max_size');
        }
        return $this->succeed($config);
    }

    public function update($siteKey, Request $request)
    {
        $data = [];
        if ($siteKey === 'website') {
            $data = $request->only(['webname','title','keywords','description','copyright']);
        }elseif ($siteKey === 'uploadConfig') {
            $data = $request->only(['storage','image_type','image_size','image_witch','image_height','image_quality','file_type','file_size']);
            !empty($data['image_type']) && $data['image_type'] = explode(',', $data['image_type']);
            !empty($data['file_type']) && $data['file_type'] = explode(',', $data['file_type']);
        }
        if (!$data) {
            return $this->error(ResponseEnum::CLIENT_DELETED_ERROR);
        }
        return Sites::UpdatePluginSet($siteKey, $data) ? $this->succeed() : $this->error();
    }

}
